<template>
  <div>
    <div class="node" v-has="{action: 'task:schedule:list', effect: 'move'}">
    <Panel title="优惠券使用限制">
      <div slot='ext' :class="$style.examine">
        <el-button v-has="{action: 'coupon:limit:add', effect: 'move'}" type="success" icon="el-icon-plus" @click="()=>edit()">新增</el-button>
        <el-button type="primary" icon="el-icon-refresh" @click="getQueryData">刷新</el-button>     
    </div>
    <TableEncapsulation
      slot="body"
      :tableConfig="tableConfig"
      :tableData="tableData"
    >
    <template slot="operation" slot-scope="operation">
          <el-button
            v-has="{action: 'coupon:limit:update', effect: 'move'}"
            type="text"
            @click="()=>edit(operation.row)"
          >编辑</el-button>
          <el-button
            v-has="{action: 'coupon:limit:delete', effect: 'move'}"
            type="text"
            @click="()=>remove(operation.row.id)"
          >删除</el-button>
        </template>
    </TableEncapsulation>
    </Panel>
     <ModalPanel 
      :visible='visible'
      title="添加/编辑"
      :onCancel="onCancel"
      :class="$style.bannerDialog"
      :onOk="onSubmit">
      <div>
        <ModifyForm
          :hiddenBtn='true'
          :editColumn='editColumn'
          :updateData="updateData"
          labelWidth='100px'
          ref="configModal"
          :class="$style.ModifyForm"
          :temporaryData='modalData'>
        </ModifyForm>
      </div>
    </ModalPanel>
    </div>
    <div style="display: none;width:100%;height:100%;margin: auto;" v-has="{action: 'task:schedule:list', effect: 'noPremiss'}">
      <NoPremission></NoPremission>
    </div>
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import SearchInput from "@/components/SearchInput";
import ModifyForm from "@/components/ModifyForm";
import ModalPanel from "@/components/ModalPanel";
export default {
  name: "task",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    SearchInput,
    ModifyForm,
    ModalPanel
  },
  data() {
    return {
      tableData: [],
      query: {
        page: 1
      },
      
      name: '',
      operation: true,
      visible:false,
      modalData:{},
      editColumn: [
        {
          name: "优惠券类型",
          prop: "type",
          type: "select",
          optionBindLabel: 'name',
          optionBindValue: 'id',
          optionList: [{'id': '','name': "请选择状态"},{'id': 1,'name': "总费用减免"},{'id':2, 'name':"服务费减免"},{'id':3, 'name':"电费减免 "}],
          onChange: () => {},
          width:24,
          required: true
        },
        {
          name: "使用数量限制",
          prop: "useNum",
          type: "number",
          width:24,
          required: true
        },
        {
          name: "使用时间(天)",
          prop: "useTime",
          type: "number",
          width:24,
          required: true
        },
      ],

    };
  },
  created() {
    this.getQueryData();
  },
  computed: {
    tableConfig () {
      return {
        selection: true,
        columnConfig: [
          {
            label: "编号",
            type: "text",
            prop: "id",
            align: "center"
          },
          {
            label: "类型",
            type: "other",
            prop: "type",
            align: "center",
            width: '120px',
            fnc: val => val == 1 ? "总费用减免" : val == 2 ? "服务费减免" : "电费减免",
            class: val => val == 1 ? "red-tag" : val == 2 ? "green-tag" : "sheng1"
          },
          {
            label: "使用张数",
            type: "text",
            prop: "useNum",
            align: "center"
          },
          {
            label: "使用时间",
            type: "text",
            prop: "useTime",
            align: "center"
          },
          this.operation && {
            type: "operation",
            align: "center",
            label: "操作",
            width:180
          }
        ].filter(Boolean)
        }
    }
  },
  methods: {
    async getQueryData() {
      const res = await request({
        url: "/coupon/limit/list",
        method: "POST",
        data: {name: this.name}
      });
      this.tableData = res.list
    },
    edit(rowData){
      this.modalData = {...rowData}
      this.visible = true
    },
    async onSubmit(){
      this.$refs['configModal'].onSubmit()
    },
     onCancel(){
      this.visible = false
    },
    async updateData(values){
      let url, msg
      if (values.id){
        url = '/coupon/limit/update'
        msg = '编辑成功'
      }else {
        url = '/coupon/limit/add'
        msg = '添加成功'
      }
    let res = await request({
      url: url,
      method: 'POST',
      data: values
    })
    if (res.code !==0) return
     this.$message.success(msg)
      this.visible = false
      this.getQueryData()
    },
    remove(id){
       this.$confirm('确认删除吗', '操作确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        let res = await request({
          url: `/coupon/limit/delete/${id}`,
          method: 'POST',
          dataType: 'json',
          data:[id]
        })
        if(res.code !==0) return
        this.$message.success('删除成功')
        this.getQueryData()
      })
    },
  }
}
</script>
<style lang='scss' module>
.examine{
    display: flex;
    align-items: center;
    :global{
        div{
            color: #fff;
            padding: 10px;
            margin: 0 10px;
            border-radius: 2px;
            font-size: 14px;
            cursor: pointer;
            min-width: 60px;
            text-align: center;
        }
        .adopt{
                background: #409EFF;
        }
    }
}
.search_box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

  :global {
    .el-input {
      width: 150px;
    }
    .el-select {
      .el-input {
        width: 90px;
      }
    }
    .el-input__icon {
      line-height: 27px;
    }
    .el-range-separator{
      line-height: 26px;
    }
  }
}

.topSelect {
  background-color: white;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}
</style>